//
//  OosMultimediaDao.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/21/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import <oos4ios/DAOs/OosMultimediaDao.h>
#import "../../Atom/Queries/OosMultimediaQuery.h"
#import "../../Atom/Feeds/OosMultimediaFeed.h"
#import "../../Atom/Entries/OosMultimediaEntry.h"
#import "../../Atom/Entries/OosImageObject.h"
#import "../../Parsers/OosMultimediaFeedParser.h"
#import "../../Parsers/OosMultimediaEntryParser.h"
#import "OosDao+Internal.h"


@implementation OosMultimediaDao

@synthesize userSlug = mUserSlug;

+ (OosMultimediaDao *) daoFromService: (OosService *) service {
	return [[[OosMultimediaDao alloc] initFromService: service] autorelease];
}

+ (OosMultimediaDao *) daoFromServiceSlug: (NSString *) serviceSlug {
	return [[[OosMultimediaDao alloc] initFromServiceSlug: serviceSlug]
	        autorelease];
}

- (OosMultimediaDao *) initFromService: (OosService *) service {
	self = [self initFromServiceSlug: [service slug]];
	if (self) {
		self->mService = [service retain];
	}
	return self;
}

- (OosMultimediaDao *) initFromServiceSlug: (NSString *) serviceSlug {
	self = [self init];
	if (self) {
		self->mServiceSlug = [serviceSlug retain];
	}
	return self;
}

- (OosQuery *) query {
	OosQuery *query = nil;
    if (!self.userSlug) {
        query = [OosMultimediaQuery queryForServiceSlug: mServiceSlug];
    } else {
        query = [OosMultimediaQuery queryForServiceSlug:mServiceSlug 
                                            andUserSlug:mUserSlug];
    }
	
	[query packStore: self.options];

	return query;
}

- (OosDaoTicket *)createFromData: (NSData *)data 
                     andMimeType: (NSString *)mimeType 
                    withCallback: (id<IOosDaoCreateCallback>)callback
{
    OosImageObject *image = [OosImageObject new];
    [image setShouldUploadDataOnly:YES];
    [image setUploadData:data];
    [image setUploadMIMEType:mimeType];
    return [self create:(OosObject *)image 
           withCallback:callback];
}

- (void)initAtomService:(OosAtomService *)atomService
{
    atomService.feedClass = [OosMultimediaFeed class];
    atomService.entryClass = [OosMultimediaEntry class];
}

- (id <IOosFeedParser>) feedParser {
	return [OosMultimediaFeedParser instance];
}

- (id<IOosEntryParser>)entryParser
{
    return [OosMultimediaEntryParser instance];
}

- (void)dealloc
{
    [mServiceSlug release];
    [mService release];
    [mUserSlug release];
    [super dealloc];
}

@end